﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace ThresholdAssistant.DataContract.Param
{
    /// <summary>
    /// Kafka消息内容
    /// </summary>
    [Serializable]
    public class KafkaMessageBody
    {
        /// <summary>
        /// 消息类型
        /// </summary>
        [JsonProperty("type"), JsonPropertyName("type")]
        public int Type { get; set; }

        /// <summary>
        /// 业务流水号
        /// </summary>
        [JsonProperty("serial_no"), JsonPropertyName("serial_no")]
        public string SerialNo { get; set; }

        /// <summary>
        /// 商家编号
        /// </summary>
        [JsonProperty("merchant_no"), JsonPropertyName("merchant_no")]
        public string MerchantNo { get; set; }

        /// <summary>
        /// 机器人微信Id
        /// </summary>
        [JsonProperty("robot_wx_id"), JsonPropertyName("robot_wx_id")]
        public string RobotWxId { get; set; }

        /// <summary>
        /// 机器人编号
        /// </summary>
        [JsonProperty("robot_serial_no"), JsonPropertyName("robot_serial_no")]
        public string RobotSerialNo { get; set; }

        /// <summary>
        /// 微信唯一编号
        /// </summary>
        [JsonProperty("device_user_serial_no"), JsonPropertyName("device_user_serial_no")]
        public string DeviceUserSerialNo { get; set; }

        /// <summary>
        /// 消息状态码
        /// </summary>
        [JsonProperty("code"), JsonPropertyName("code")]
        public string Code { get; set; }

        /// <summary>
        /// 消息详情
        /// </summary>
        [JsonProperty("message"), JsonPropertyName("message")]
        public string Message { get; set; }

        /// <summary>
        /// 数据详情
        /// </summary>
        [JsonProperty("data"), JsonPropertyName("data")]
        public object Data { get; set; }

        /// <summary>
        /// 头部信息
        /// </summary>
        [JsonProperty("headers"), JsonPropertyName("headers")]
        public Dictionary<string, string> Headers { get; set; }

        /// <summary>
        /// 获取消息内容
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetData<T>()
        {
            T result = default;
            if (Data == null)
                return result;

            var data = Data.ToString();
            if (string.IsNullOrEmpty(data))
                return result;

            return JsonConvert.DeserializeObject<T>(data);
        }
    }
}